package com.frcdi.qms.measurement.service.impl;

import com.frcdi.qms.common.constant.UserConstants;
import com.frcdi.qms.common.utils.DateUtils;
import com.frcdi.qms.common.utils.StringUtils;
import com.frcdi.qms.masterdata.domain.CodeConfig;
import com.frcdi.qms.masterdata.mapper.CodeConfigMapper;
import com.frcdi.qms.measurement.domain.MeasurementBalance;
import com.frcdi.qms.measurement.mapper.MeasurementBalanceMapper;
import com.frcdi.qms.measurement.service.IMeasurementBalanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 一般性测量(天平)Service业务层处理
 *
 * @author cell
 * @date 2024-02-26
 */
@Service
public class MeasurementBalanceServiceImpl implements IMeasurementBalanceService
{
    @Autowired
    private MeasurementBalanceMapper measurementBalanceMapper;

    @Autowired
    private CodeConfigMapper codeMapper;

    /**
     * 查询一般性测量(天平)
     *
     * @param measurementId 一般性测量(天平)主键
     * @return 一般性测量(天平)
     */
    @Override
    public MeasurementBalance selectMeasurementBalanceByMeasurementId(Long measurementId)
    {
        return measurementBalanceMapper.selectMeasurementBalanceByMeasurementId(measurementId);
    }

    /**
     * 查询一般性测量(天平)列表
     *
     * @param measurementBalance 一般性测量(天平)
     * @return 一般性测量(天平)
     */
    @Override
    public List<MeasurementBalance> selectMeasurementBalanceList(MeasurementBalance measurementBalance)
    {
        return measurementBalanceMapper.selectMeasurementBalanceList(measurementBalance);
    }

    /**
     * 新增一般性测量(天平)
     *
     * @param measurementBalance 一般性测量(天平)
     * @return 结果
     */
    @Override
    public String insertMeasurementBalance(MeasurementBalance measurementBalance)
    {
        measurementBalanceMapper.insertMeasurementBalance(measurementBalance);
        return measurementBalance.getMeasurementId().toString();
    }

    /**
     * 修改一般性测量(天平)
     *
     * @param measurementBalance 一般性测量(天平)
     * @return 结果
     */
    @Override
    public int updateMeasurementBalance(MeasurementBalance measurementBalance)
    {
        return measurementBalanceMapper.updateMeasurementBalance(measurementBalance);
    }

    /**
     * 批量删除一般性测量(天平)
     *
     * @param measurementIds 需要删除的一般性测量(天平)主键
     * @return 结果
     */
    @Override
    public int deleteMeasurementBalanceByMeasurementIds(Long[] measurementIds)
    {
        return measurementBalanceMapper.deleteMeasurementBalanceByMeasurementIds(measurementIds);
    }

    /**
     * 删除一般性测量(天平)信息
     *
     * @param measurementId 一般性测量(天平)主键
     * @return 结果
     */
    @Override
    public int deleteMeasurementBalanceByMeasurementId(Long measurementId)
    {
        return measurementBalanceMapper.deleteMeasurementBalanceByMeasurementId(measurementId);
    }

    @Override
    public String getCode() {
        String syscode = new StringBuilder().toString();
        // 获取前缀
        CodeConfig codeConfig = codeMapper.selectCodeConfigByCodeId(5L);
        if (StringUtils.equals(UserConstants.CODE_ADDTIME,codeConfig.getCodeTime())){
            syscode = codeConfig.getCodeRule()+DateUtils.dateTime();
        }else {
            syscode = codeConfig.getCodeRule();
        }
        // 获取最近一次生成
        String code = measurementBalanceMapper.getCode(syscode);

        if (StringUtils.isNull(code)){
            return syscode + StringUtils.padl(1,codeConfig.getCodeNumber());
        }

        String newcode = StringUtils.substring(code, code.length() - codeConfig.getCodeNumber(), code.length());
        return syscode + (StringUtils.padl(Integer.valueOf(newcode)+1,codeConfig.getCodeNumber()));
    }
}
